package com.woniuxy.controller;

import cn.hutool.core.util.StrUtil;
import com.nimbusds.jose.JOSEException;
import com.woniuxy.annotation.PassLevelAnnotation;
import com.woniuxy.entity.dto.system.UserDTO;
import com.woniuxy.entity.po.system.Role;
import com.woniuxy.entity.po.system.User;
import com.woniuxy.service.UserService;
import com.woniuxy.util.CodeUtil;
import com.woniuxy.util.PageParam;
import com.woniuxy.util.ResponseData;
import com.woniuxy.util.ResponseEnum;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.text.ParseException;
import java.util.List;

/**
 * @BelongsProject: charging-management-platform
 * @BelongsPackage: com.woniuxy.controller
 * @Author: Quan
 * @Date: 2025/3/27 9:15
 */
@RestController
@RequestMapping("/user")
@AllArgsConstructor
public class UserController {
	private UserService userService;
	private CodeUtil codeUtil;

	@PostMapping("/login")
	public ResponseData<?> login(@RequestBody User user) {
		try {
			// 判断是否为空
			if (StrUtil.isBlank(user.getUsername()) || StrUtil.isBlank(user.getPassword())) {
				return ResponseData.fail(ResponseEnum.PARAM_ERROR);
			}
			return ResponseData.ok(userService.login(user));
		} catch (JOSEException e) {
			throw new RuntimeException(e);
		}
	}
	@PostMapping("/queryAll")
	public ResponseData<?> queryAll(@RequestBody PageParam<UserDTO> pageParam){
		return userService.queryAll(pageParam);
	}
	@GetMapping("/deleteUser/{id}")
	public ResponseData<?> deleteUser(@PathVariable("id")Long id){
		userService.deleteUser(id);
		return ResponseData.ok();
	}
	@PostMapping("/batchDeleteUser")
	public ResponseData<?> batchDeleteUser(@RequestBody List<Long> ids){
		userService.batchDeleteUser(ids);
		return ResponseData.ok();
	}
	@PostMapping("/addUser")
	public ResponseData<?> addUser(@RequestBody User user){
		userService.addUser(user);
		return ResponseData.ok();
	}
	@GetMapping("/queryIdInfo/{id}")
	public ResponseData<?> queryIdInfo(@PathVariable("id")Long id){
		return ResponseData.ok(userService.queryIdInfo(id));
	}
	@PostMapping("/updateUser")
	public ResponseData<?> updateUser(@RequestBody UserDTO userDTO){
		userService.updateUser(userDTO);
		return ResponseData.ok();
	}
	@GetMapping("/deptClean/{deptId}")
	@PassLevelAnnotation(passLevel = 3)
	public ResponseData<?> deptClean(@PathVariable("deptId")Long deptId){
		userService.deptClean(deptId);
		return ResponseData.ok();
	}
	@GetMapping("/getCode")
	public ResponseData<?> getCode() {
		return ResponseData.ok(codeUtil.generateRegularCode("USER"));
	}
	/**
	 * @Description: 上传图片，并返回地址
	 * @Param: [file]
	 * @return: com.woniuxy.util.ResponseData<?>
	 * @Author: Quan
	 * @Date: 2025/4/2
	 */
	@PostMapping("/upload")
	public ResponseData<?> uploadFile(MultipartFile file) {
		try {
			return userService.uploadImg(file);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	@GetMapping("/getImgSrc")
	public ResponseData<?> getImgSrc() {
		try {
			return userService.getImgSrc();
		} catch (ParseException e) {
			throw new RuntimeException(e);
		}
	}
}
